home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / load-unl.000 / load-unl / load-unload-1.1 / unload.c < prev   
Encoding:
C/C++ Source or Header  |  1995-08-28  |  3.6 KB  |  186 lines

  1. /*$VER: UnLoad v1.1 - (c) 1995 - JPM*/
  2. /*
  3.    UnLoad - A small program to Load/Unload SCSI devices using the generic
  4.             SCSI interface.
  5.  
  6.    Author:  Juergen P. Meier -  Gauting, Germany
  7.             (jor@muecke.rz.fh-muenchen.de)
  8.  
  9.    License: This file may be copied under the terms and conditions of
  10.             version 2 of the GNU General Public License, as published
  11.         by the Free Software Foundation (Cambridge, Massachusetts).
  12.  
  13.    Initial release v1.0.
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <sys/file.h>
  20. #include <unistd.h>
  21.  
  22. #define __KERNEL__
  23. #include <linux/fs.h>
  24. #undef __KERNEL__
  25. #include "block/blk.h"
  26.  
  27. #include "scsi/scsi.h"
  28. #include "scsi/sg.h"
  29. #include <string.h>
  30.  
  31. struct sg_request {
  32.   struct sg_header header;
  33.   unsigned char bytes[100];
  34. } sg_request;
  35.  
  36. struct sg_reply {
  37.   struct sg_header header;
  38.   unsigned char bytes[100];
  39. };
  40.  
  41. void usage(char *name)
  42. {
  43.   printf(" Usage: %s [device] [0|1]\n", name);
  44.   printf("        0 - load Medium (tested: TOSHIBA XM3601 CD-ROM)\n");
  45.   printf("            default if _not_ invoked as 'unload' or 'eject'\n");
  46.   printf("        1 - eject Medium (should work with any drive)\n");
  47.   printf("            default if invoked as 'unload' or 'eject'\n");
  48.   printf("   device - opt. SCSI-generic device (default: /dev/sgc)\n");
  49.   printf(" UnLoad v1.0 - (c) 1995 Juergen P. Meier\n");
  50.   return;
  51. }
  52.  
  53. void main(int argc, char **argv)
  54. {
  55.   int fd_sg;
  56.   int unload = 0;
  57.   struct sg_reply reply;
  58.   struct sg_request request;
  59.   int reply_len, exp_size, act_size;
  60.   char sg_def[] = "/dev/sgc";
  61.   char *sg_name = sg_def;
  62.  
  63.   if (strcmp(argv[0],"unload") == 0)
  64.     unload = 1;
  65.   if (strcmp(argv[0],"eject") == 0)
  66.     unload = 1;
  67.  
  68.   if (argc > 3)
  69.   {
  70.     printf("%s: too many arguments\n", argv[0]);
  71.     usage(argv[0]);
  72.     exit (10);
  73.   }
  74.   if (argc == 2)
  75.   {
  76.     if (argv[1][1] == 0)
  77.     {
  78.       switch (*argv[1])
  79.       {
  80.         case '0':
  81.     {
  82.       unload = 0;
  83.       break;
  84.     }
  85.         case '1':
  86.     {
  87.       unload = 1;
  88.       break;
  89.     }
  90.         default:
  91.     {
  92.       printf("%s: invalid argument -- %s\n", argv[0], argv[1]);
  93.       usage(argv[0]);
  94.       exit (9);
  95.     }
  96.       } /* switch */
  97.     }
  98.     else /* devicename */
  99.     {
  100.       sg_name = argv[1];
  101.     }
  102.   } /* if (argc == 2) */
  103.   if (argc == 3)
  104.   {
  105.     if (argv[2][1] == 0)
  106.     {
  107.       switch (*argv[2])
  108.       {
  109.         case '0':
  110.     {
  111.           unload = 0;
  112.           break;
  113.         }
  114.       case '1':
  115.         {
  116.           unload = 1;
  117.           break;
  118.         }
  119.       default:
  120.         {
  121.           printf("%s: invalid argument -- %s\n", argv[0], argv[2]);
  122.           usage(argv[0]);
  123.           exit (8);
  124.         }
  125.       } /* switch */
  126.     }
  127.     sg_name = argv[1];
  128.   } /* if (argc == 3) */
  129.  
  130.   if ( (fd_sg = open(sg_name, O_RDWR)) < 0 )
  131.   {
  132.     printf("%s: error %d opening generic scsi device: %s\n",
  133.        argv[0], fd_sg, sg_name);
  134.     exit (7);
  135.   }
  136.  
  137.   reply_len = sizeof(struct sg_reply);
  138.   bzero (&reply, sizeof(reply_len));
  139.   bzero (&request, sizeof(request));
  140.  
  141.   request.header.pack_len = sizeof(struct sg_header) + 6;
  142.   request.header.reply_len = reply_len + sizeof(struct sg_header);
  143.   request.header.pack_id = 0;
  144.   request.header.result = 0;
  145.  
  146.   request.bytes[0] = 0x1B;
  147.   request.bytes[1] = 0;
  148.   request.bytes[2] = 0;
  149.   request.bytes[3] = 0;
  150.   request.bytes[4] = unload ? 2 : 3;
  151.   request.bytes[5] = 0;
  152.  
  153.   exp_size = sizeof(struct sg_header) + 6;
  154.   act_size = write(fd_sg, &request, exp_size);
  155.   if (act_size < 0)
  156.   {
  157.     printf("%s: write failed", argv[0]);
  158.     exit (5);
  159.   }
  160.   else if (act_size != exp_size)
  161.   {
  162.     printf("%s: wrote %d bytes, expected to write %d.\n",
  163.        argv[0], act_size, exp_size);
  164.   }
  165.  
  166.   reply_len = read(fd_sg, &reply, sizeof(struct sg_reply));
  167.  
  168. /*  printf("done\n"); */
  169.  
  170.   close(fd_sg);
  171.   exit (0);
  172. }
  173. /* EOF */
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.